clearShirtHTML = "<div class='ismShirtContainer' ><img src='../images/background/shirt0.png' " +
		"width='48' height='63' alt='' title='' class='ismJsShirt ismShirt ismShirtPitch ismShirtZero' " +
		"/></div>"; // html for removing player from pitch 

goalkeeper = 1; // count of maximum goalkeepers
defendersCount = 4; // count of maximum defenders
midfieldersCount = 4; // count of maximum midfielders
strikersCount = 2; // count of maximum strikers

playersSelectedCount = 0; // count of already selected players

alreadySelectedPlayers = new Array(); // array with already selected players

defenders = new Array( 0, 0, 0, 0); 
midfielders = new Array( 0, 0, 0, 0);
strikers = new Array( 0, 0);

function implementAjax() {
	var request = null;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera,
		// Safari
		request = new XMLHttpRequest();
	} else {// code for IE6, IE5
		request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return request;
}

function printPlayersSelectedCount(){
	document.getElementById("ismNumSelected").innerHTML = playersSelectedCount;
}

function isPlayerSelected(player_no){
	var inArray = false;
	for(var j = 0; j < alreadySelectedPlayers.length; j++){
		if(alreadySelectedPlayers[j] == player_no)
			inArray = true;
	}
	return inArray;
}

function removePlayerIdFromAlreadySelectedPlayers(player_no){
	var i = alreadySelectedPlayers.indexOf(player_no);
	alreadySelectedPlayers.splice(i, 1);
}

function printBudget(operator, playerValue){
	if(operator == "-"){
		var playerPrice = parseFloat((playerValue));
		var remainingBudget = parseFloat(document.getElementById("remainingBudget").innerHTML);
		var remaining = remainingBudget - playerPrice;
	}
	else {
		var playerPrice = parseFloat(playerValue);
		var remainingBudget = parseFloat(document.getElementById("remainingBudget").innerHTML);
		var remaining = remainingBudget + playerPrice;
	}
	document.getElementById("remainingBudget").innerHTML = remaining.toFixed(1);
	if(remaining < 0){
		alert("Attention! Not enough money!");
	}
}

function setGoalkeeper(player_no, playerValue) {
	if (goalkeeper) {
		request = implementAjax();
		request.onreadystatechange = function() {
			if (request.readyState == 4 && request.status == 200) {
				document.getElementById("goalkeeper").innerHTML = request.responseText;
				goalkeeper--;
				playersSelectedCount++;
				document.getElementById("ismNumSelected").innerHTML = playersSelectedCount;
				printBudget("-", playerValue);
				removeDisabled();
			}
		}
		var url = "GenerateShirt.php?playerno=" + player_no;
		request.open("GET", url, true);
		request.send();
	} else {
		alert("You can't select more goalkeepers!");
	}
}

function clearGoalkeeperShirt(playerValue) {
	printBudget("+", playerValue);
	document.getElementById("goalkeeper").innerHTML = clearShirtHTML;
	goalkeeper++;
	playersSelectedCount--;
	printPlayersSelectedCount();
	removeDisabled();
}

function setDefender(player_no, playerValue) {
	if (defendersCount) {
		if (!isPlayerSelected(player_no)) {
			i = 0;
			for (i; i < defenders.length; i++) {
				if (defenders[i] == 0) {

					request = implementAjax();
					request.onreadystatechange = function() {
						if (request.readyState == 4 && request.status == 200) {
							document.getElementById("defender" + i).innerHTML = request.responseText;
							defenders[i] = 1;
							defendersCount--;
							playersSelectedCount++;
							printPlayersSelectedCount();
							printBudget("-", playerValue);
							alreadySelectedPlayers.push(player_no);
							removeDisabled();
						}
					}

					var url = "GenerateShirt.php?playerno=" + player_no
							+ "&id=" + i;
					request.open("GET", url, true);
					request.send();
					break;
				}
			}
		} else {
			alert("You have already selected this player!");
		}
	} else {
		alert("You can't add more defenders!");
	}
}

function clearDefenderShirt(id, player_no, playerValue){
	printBudget("+", playerValue);
	document.getElementById("defender"+id).innerHTML = clearShirtHTML;
	defenders[id] = 0;
	defendersCount++;
	playersSelectedCount--;
	printPlayersSelectedCount();
	removePlayerIdFromAlreadySelectedPlayers(player_no);
	removeDisabled();
}

function setMidfielder(player_no, playerValue){
	if(midfieldersCount){
		if (!isPlayerSelected(player_no)) {
	i = 0;
	for(i; i < midfielders.length; i++){
		if(midfielders[i] == 0){

		request = implementAjax();
		request.onreadystatechange = function() {
			if (request.readyState == 4 && request.status == 200) {
				document.getElementById("midfielder"+i).innerHTML = request.responseText;
				midfielders[i] = 1;
				midfieldersCount--;
				playersSelectedCount++;
				printPlayersSelectedCount();
				printBudget("-", playerValue);
				alreadySelectedPlayers.push(player_no);
				removeDisabled();
				}
			}
		
		var url = "GenerateShirt.php?playerno=" + player_no +"&id="+i;
		request.open("GET", url, true);
		request.send();
		break;
		}
	}
	
	} else {
		alert("You have already selected this player!");
	}
	}
	else{
		alert("You can't add more midfielders!");
	}
}

function clearMidfielderShirt(id, player_no, playerValue){
	printBudget("+", playerValue);
	document.getElementById("midfielder"+id).innerHTML = clearShirtHTML;
	midfielders[id] = 0;
	midfieldersCount++;
	playersSelectedCount--;
	printPlayersSelectedCount();
	removePlayerIdFromAlreadySelectedPlayers(player_no);
	removeDisabled();
}

function setStriker(player_no, playerValue){
	if(strikersCount){
		if (!isPlayerSelected(player_no)) {
	i = 0;
	for(i; i < strikers.length; i++){
		if(strikers[i] == 0){

		request = implementAjax();
		request.onreadystatechange = function() {
			if (request.readyState == 4 && request.status == 200) {
				document.getElementById("striker"+i).innerHTML = request.responseText;
				strikers[i] = 1;
				strikersCount--;
				playersSelectedCount++;
				printPlayersSelectedCount();
				printBudget("-", playerValue);
				alreadySelectedPlayers.push(player_no);
				removeDisabled();
				}
			}
		
		var url = "GenerateShirt.php?playerno=" + player_no +"&id="+i;
		request.open("GET", url, true);
		request.send();
		break;
		}
	}
	
	} else {
		alert("You have already selected this player!");
	}
	}
	else{
		alert("You can't add more strikers!");
	}
	
}

function clearStrikerShirt(id, player_no, playerValue){
	printBudget("+", playerValue);
	document.getElementById("striker"+id).innerHTML = clearShirtHTML;
	strikers[id] = 0;
	strikersCount++;
	playersSelectedCount--;
	printPlayersSelectedCount();
	removePlayerIdFromAlreadySelectedPlayers(player_no);
	removeDisabled();
}

function updateFilterList(filter) {
	request = implementAjax();
	request.onreadystatechange = function() {
		if (request.readyState == 4 && request.status == 200) {
			document.getElementById("ismElementDataTable").innerHTML = request.responseText;
		}
	}
	var url = "FilterPlayers.php?filter=" + filter;
	request.open("GET", url, true);
	request.send();
}

function removeDisabled(){
	if(playersSelectedCount != 11 || parseFloat(document.getElementById("remainingBudget").innerHTML) < 0){
		document.getElementById("submitSquad").disabled = true;
	}
	else{
		document.getElementById("submitSquad").disabled = false;
	}
}

function resetSquad(){
	goalkeeper = 1;
	defendersCount = 4;
	midfieldersCount = 4;
	strikersCount = 2;
	playersSelectedCount = 0;
	
	document.getElementById("goalkeeper").innerHTML = clearHTML;
	for(var i = 0; i < defenders.length; i++){
		defenders[i] = 0;
		document.getElementById("defender"+i).innerHTML = clearHTML;
		midfielders[i] = 0;
		document.getElementById("midfielder"+i).innerHTML = clearHTML;
	}
	for(var i = 0; i < strikers.length; i++){
		strikers[i] = 0;
		document.getElementById("striker"+i).innerHTML = clearHTML;
	}
	document.getElementById("ismNumSelected").innerHTML = 80.0; 
	goalkeeper = 1;
	defendersCount = 4;
	midfieldersCount = 4;
	strikersCount = 2;
	playersSelectedCount = 0;
	alreadySelectedPlayers = [];
}

function activateReset(){
	if(playersSelectedCount != 0){
		document.getElementById("resetSquad").disabled = false;
	}
	else {
		document.getElementById("resetSquad").disabled = true;
	}
}
